#!/usr/bin/env python
import sys
import argparse
import logging
import psutil
from sensor_control import get_raspberry_pi_model
from main import main as run_main

def setup_logging():
    """配置日志系统"""
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
        handlers=[
            logging.FileHandler('firealarm.log'),
            logging.StreamHandler(sys.stdout)
        ]
    )

def parse_arguments():
    """解析命令行参数"""
    parser = argparse.ArgumentParser(description='火灾报警系统主程序')
    parser.add_argument('-s', '--simple', action='store_true', help='以简单CLI模式运行，不启动图形界面')
    return parser.parse_args()

if __name__ == '__main__':
    setup_logging()
    args = parse_arguments()
    
    # 检测树莓派型号
    model = get_raspberry_pi_model()
    if model not in ['pi4', 'pi5']:
        logging.error(f"不支持的硬件型号: {model}。仅支持树莓派4/5。")
        sys.exit(1)

    # 设置进程优先级
    try:
        process = psutil.Process()
        process.nice(-10)  # Linux系统设置为高优先级
        logging.info("程序优先级已提高")
    except Exception as e:
        logging.warning(f"无法设置进程优先级: {e}")
        
    run_main(simple_mode=args.simple)